package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.utils.MQTTConnect;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
public class MqttController {

    @Resource
    MQTTConnect mqttConnect;

    //服务器远程开井
    @RequestMapping("/RemoteOpenWell")
    @ResponseBody
    public String remoteOpenWell() throws JsonProcessingException, MqttException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cmd",6);
        jsonObject.put("ver","v_1.0");

        JSONObject data = new JSONObject();
        data.put("user_id_card","371202199811179999");
        data.put("user_balance",100.00);

        jsonObject.put("data",data);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(jsonObject);
        boolean pub = mqttConnect.pub("/single/down/869329061412899", s);//发布主题

        return "成功";
    }

    //5.4远程开泵  wd/设备编号/remoteOpen
    @PostMapping("/ImportWdDeviceEemoteOpenSrv")
    @ResponseBody
    public String importWdDeviceEemoteOpenSrv(@RequestBody String json) throws JsonProcessingException, MqttException {
        Map map = JSON.parseObject(json, Map.class);
        Map check = checkRemoteOpen(map);
        //结果集
        Map result = new LinkedHashMap();
        //工具类
        ObjectMapper objectMapper = new ObjectMapper();

        if (check != null) {
            String string = MapUtils.getString(check, "500");
            result.put("status","false");
            result.put("errorMsg",string);
            return objectMapper.writeValueAsString(result);
        }
        //封装数据
        JSONObject object = new JSONObject();
        object.put("cmd","control_open");
        object.put("data",map);

        String s = objectMapper.writeValueAsString(object);
        boolean pub = mqttConnect.pub("wd/remoteOpen", s);//发布主题
        result.put("status","true");
        result.put("msg","已发送！");
        return JSON.toJSONString(result);

    }

    /**
     * 校验
     * @param jsonMap
     * @return
     */
    public Map checkRemoteOpen(Map jsonMap) {
        Map error = new LinkedHashMap();
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cDevId", ""))) {
            error.put("500", "设备ID未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cCode", ""))) {
            error.put("500", "执行编号未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cardType", ""))) {
            error.put("500", "刷卡类型未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cardNo", ""))) {
            error.put("500", "卡号未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cOpenTime", ""))) {
            error.put("500", "开泵时间未填写");
            return error;
        }
        Double cMoney = MapUtils.getDouble(jsonMap, "cMoney");
        if (cMoney == null) {
            error.put("500", "卡余额未填写");
            return error;
        }
        return null;
    }


    //5.5远程关泵   wd/设备编号/remoteClose
    @PostMapping("/ImportWdDeviceEemoteCloseSrv")
    @ResponseBody
    public String importWdDeviceEemoteCloseSrv(@RequestBody String json) throws JsonProcessingException, MqttException {
        Map map = JSON.parseObject(json, Map.class);
        Map check = checkRemoteClose(map);
        //结果集
        Map result = new LinkedHashMap();
        //工具类
        ObjectMapper objectMapper = new ObjectMapper();

        if (check != null) {
            String string = MapUtils.getString(check, "500");
            result.put("status","false");
            result.put("errorMsg",string);
            return objectMapper.writeValueAsString(result);
        }

        //封装数据
        JSONObject object = new JSONObject();
        object.put("cmd","control_close");
        object.put("data",map);

        String s = objectMapper.writeValueAsString(object);
        boolean pub = mqttConnect.pub("wd/remoteClose", s);//发布主题
        result.put("status","true");
        result.put("msg","已发送！");
        return JSON.toJSONString(result);

    }

    /**
     * 校验
     * @param jsonMap
     * @return
     */
    public Map checkRemoteClose(Map jsonMap) {
        Map error = new LinkedHashMap();
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cDevId", ""))) {
            error.put("500", "设备ID未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cCode", ""))) {
            error.put("500", "执行编号未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cardType", ""))) {
            error.put("500", "刷卡类型未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cardNo", ""))) {
            error.put("500", "卡号未填写");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "closeTime", ""))) {
            error.put("500", "持关泵时间未填写");
            return error;
        }
        Double totalMoney = MapUtils.getDouble(jsonMap, "totalMoney");
        if (totalMoney == null) {
            error.put("500", "消费前金额未填写");
            return error;
        }
        Double fee = MapUtils.getDouble(jsonMap, "fee");
        if (fee == null) {
            error.put("500", "本次消费金额未填写");
            return error;
        }
        Double money = MapUtils.getDouble(jsonMap, "money");
        if (money == null) {
            error.put("500", "卡余额未填写");
            return error;
        }
        Double uele = MapUtils.getDouble(jsonMap, "uele");
        if (uele == null) {
            error.put("500", "本次用电消耗 kwh未填写");
            return error;
        }
        Double uelePrice = MapUtils.getDouble(jsonMap, "uelePrice");
        if (uelePrice == null) {
            error.put("500", "电价未填写");
            return error;
        }
        Double uwater = MapUtils.getDouble(jsonMap, "uwater");
        if (uwater == null) {
            error.put("500", "本次用水消耗 m³未填写");
            return error;
        }
        Double uwaterPrice = MapUtils.getDouble(jsonMap, "uwaterPrice");
        if (uwaterPrice == null) {
            error.put("500", "水价未填写");
            return error;
        }
        Integer utime = MapUtils.getInteger(jsonMap, "utime");
        if (utime == null) {
            error.put("500", "本次用时 单位：秒未填写");
            return error;
        }
        return null;
    }

//    //5.6 虚拟卡充值   pay/虚拟卡号/unrealAdd
//    @PostMapping("/ImportWdDeviceUnrealAddSrv")
//    @ResponseBody
//    public String importWdDeviceUnrealAddSrv(@RequestBody String json) throws JsonProcessingException, MqttException {
//        Map map = JSON.parseObject(json, Map.class);
//        Map check = checkRemoteClose(map);
//        //结果集
//        Map result = new LinkedHashMap();
//        //工具类
//        ObjectMapper objectMapper = new ObjectMapper();
//
//        if (check != null) {
//            String string = MapUtils.getString(check, "500");
//            result.put("status","false");
//            result.put("errorMsg",string);
//            return objectMapper.writeValueAsString(result);
//        }
//
//        String s = objectMapper.writeValueAsString(map);
//        boolean pub = mqttConnect.pub("wd/remoteClose", s);//发布主题
//        result.put("status","true");
//        result.put("msg","已发送！");
//        return JSON.toJSONString(result);
//
//    }


    /**
     * 校验
     * @param jsonMap
     * @return
     */
//    public Map checkUnrealAdd(Map jsonMap) {
//        Map error = new LinkedHashMap();
//        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cardNo", ""))) {
//            error.put("500", "卡号未填写");
//            return error;
//        }
//        Double addAmount = MapUtils.getDouble(jsonMap, "addAmount");
//        if (addAmount == null) {
//            error.put("500", "充值金额未填写");
//            return error;
//        }
//        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "deviceNo", ""))) {
//            error.put("500", "设备编号未填写");
//            return error;
//        }
//        Double money = MapUtils.getDouble(jsonMap, "money");
//        if (money == null) {
//            error.put("500", "余额（充值金额+卡余额）未填写");
//            return error;
//        }
//        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "time", ""))) {
//            error.put("500", "充值时间未填写");
//            return error;
//        }
//        return null;
//    }

    //5.6.1实体卡充值   pay/实体卡号/entityAdd
//    @PostMapping("/ImportWdDeviceEntityAddSrv")
//    @ResponseBody
//    public String importWdDeviceEntityAddSrv(@RequestBody String json) throws JsonProcessingException, MqttException {
//        Map map = JSON.parseObject(json, Map.class);
//        Map check = checkRemoteClose(map);
//        //结果集
//        Map result = new LinkedHashMap();
//        //工具类
//        ObjectMapper objectMapper = new ObjectMapper();
//
//        if (check != null) {
//            String string = MapUtils.getString(check, "500");
//            result.put("status","false");
//            result.put("errorMsg",string);
//            return objectMapper.writeValueAsString(result);
//        }
//
//        String cardNo = MapUtils.getString(map, "cardNo", "");
//
//
//        String s = objectMapper.writeValueAsString(map);
//        boolean pub = mqttConnect.pub("wd/" + cardNo + "/remoteClose", s);//发布主题
//        result.put("status","true");
//        result.put("msg","已发送！");
//        return JSON.toJSONString(result);
//
//    }

    /**
     * 校验
     * @param jsonMap
     * @return
     */
//    public Map checkEntityAdd(Map jsonMap) {
//        Map error = new LinkedHashMap();
//        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "cardNo", ""))) {
//            error.put("500", "卡号未填写");
//            return error;
//        }
//        Double addAmount = MapUtils.getDouble(jsonMap, "addAmount");
//        if (addAmount == null) {
//            error.put("500", "充值金额未填写");
//            return error;
//        }
//        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "deviceNo", ""))) {
//            error.put("500", "设备编号未填写");
//            return error;
//        }
//        Double money = MapUtils.getDouble(jsonMap, "money");
//        if (money == null) {
//            error.put("500", "余额（充值金额+卡余额）未填写");
//            return error;
//        }
//        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "time", ""))) {
//            error.put("500", "充值时间未填写");
//            return error;
//        }
//        Integer addType = MapUtils.getInteger(jsonMap, "addType");
//        if (addType == null) {
//            error.put("500", "充值类型未填写");
//            return error;
//        }
//        return null;
//    }


}
